﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;

namespace Efficient.Excel
{
    internal class BooleanReaderValueGetter : IDataReaderValueGetter
    {
        const string trueField = "true";
        private readonly string trueString = "true";
        public BooleanReaderValueGetter(string valueList)
        {
            string[] valGroup = valueList.Split(',');
            bool valueInited = false;
            foreach (string item in valGroup)
            {
                string[] keyVal = item.Split('=');
                if (keyVal.Length == 2)
                {
                    if (keyVal[0].Trim().Equals(trueField, StringComparison.OrdinalIgnoreCase))
                    {
                        trueString = keyVal[1].Trim();
                        valueInited = true;
                    }
                }
            }
            if (!valueInited)
            {
                throw new ArgumentException("布尔读取器初始化失败！不能从:" + valueList + " 识别true字符串");
            }
        }

        public object GetValue(IDataReader reader, string name)
        {
            return GetValue(reader, reader.GetOrdinal(name));
        }

        public object GetValue(IDataReader reader, int index)
        {
            string val = reader.GetValue(index).ToString();
            return val.Equals(trueString, StringComparison.OrdinalIgnoreCase);
        }
    }
}
